{"nbformat":4,"nbformat_minor":0,"metadata":{"kernelspec":{"name":"python36","display_name":"Python 3.6","language":"python"},"language_info":{"mimetype":"text/x-python","nbconvert_exporter":"python","name":"python","pygments_lexer":"ipython3","version":"3.6.6","file_extension":".py","codemirror_mode":{"version":3,"name":"ipython"}},"colab":{"name":"Python for Ren'Py Developers.ipynb","provenance":[{"file_id":"1q-r1EnTVH0Z5tNu8ecRyHcMJtuLCXY_p","timestamp":1623607138544},{"file_id":"1pVPQ6lTI7oKS7bbcBZLogBHNW7LDxFIX","timestamp":1572041406478}],"collapsed_sections":["ji8ou83GQjp2","VPM_K2jrQjpL","xBRIg8sXXzIa","i66-ZYDVQjrG","SJ3d92h2JOkG","RDlIm1PVVYcN","4DWBzytZWqGs","40NwTDl3XswK","IMRHc91WY6CN","pi8MpCxnYH2z"]}},"cells":[{"cell_type":"markdown","metadata":{"id":"lD4qZ7bGQjpG"},"source":["# Python for Ren'Py Developers\n","\n","<div><img src=\"https://www.renpy.org/static/index-logo.png\"><img src=\"https://www.python.org/static/img/python-logo.png\"></div>"]},{"cell_type":"markdown","metadata":{"id":"g5w2OMs_vHqZ"},"source":["## Outline\n","\n","Variables\n","\n","- Strings\n","- Numbers\n","- Booleans\n","\n","Data Structures\n","\n","- Lists\n","- Sets\n","- Dictionaries\n","\n","Statements\n","\n","- Conditional\n","- Loop\n","- Looping over iterables\n","\n","Advanced\n","\n","- Functions\n","- Classes"]},{"cell_type":"markdown","metadata":{"id":"ji8ou83GQjp2"},"source":["## Strings"]},{"cell_type":"markdown","metadata":{"id":"tLpdy3AgQjp4"},"source":["We represent text in Python using strings. Strings can be enclosed in single quotes (`'...'`) or double quotes (`\"...\"`) with the same result."]},{"cell_type":"code","metadata":{"trusted":true,"id":"thaA7-gPQjp4","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859429244,"user_tz":420,"elapsed":148,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"d117b2b1-14eb-42b6-a940-2ddc6e2bca61"},"source":["'spam eggs'  # Single quotes."],"execution_count":9,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'spam eggs'"]},"metadata":{"tags":[]},"execution_count":9}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"oh8mIJ85aqJv","executionInfo":{"status":"ok","timestamp":1623859424899,"user_tz":420,"elapsed":207,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"7bf4a688-465f-4d41-e00f-fb1570bd742b"},"source":["\"spam eggs\""],"execution_count":7,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'spam eggs'"]},"metadata":{"tags":[]},"execution_count":7}]},{"cell_type":"markdown","metadata":{"id":"SWOWxK_FgKVG"},"source":["We use `\\` to escape quotes, that is, to use a quote within the string itself:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"-XJ1oZXPQjp6","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859448167,"user_tz":420,"elapsed":204,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"3b7f4509-8e03-465e-adf0-16ab0611ca8e"},"source":["'doesn\\'t'  # Use \\' to escape the single quote..."],"execution_count":10,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["\"doesn't\""]},"metadata":{"tags":[]},"execution_count":10}]},{"cell_type":"code","metadata":{"trusted":true,"id":"sB454uAqQjp7","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859455231,"user_tz":420,"elapsed":210,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"6d5e4328-3800-4545-856a-ebf40779266e"},"source":["\"doesn't\"  # ...or use double quotes instead."],"execution_count":12,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["\"doesn't\""]},"metadata":{"tags":[]},"execution_count":12}]},{"cell_type":"code","metadata":{"trusted":true,"id":"28g3V0ZZQjp9","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859459673,"user_tz":420,"elapsed":173,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"ac9c6eb6-de08-4219-c8d2-412b08a6e804"},"source":["'\"Yes,\" he said.'"],"execution_count":13,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'\"Yes,\" he said.'"]},"metadata":{"tags":[]},"execution_count":13}]},{"cell_type":"code","metadata":{"trusted":true,"id":"N0XZIrGmQjp-","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859465066,"user_tz":420,"elapsed":249,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"47b633c5-d5bc-4316-b746-79abe36f6f94"},"source":["\"\\\"Yes,\\\" he said.\""],"execution_count":14,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'\"Yes,\" he said.'"]},"metadata":{"tags":[]},"execution_count":14}]},{"cell_type":"markdown","metadata":{"id":"HcZ4HxtNwL0u"},"source":["`\\n` starts a newline"]},{"cell_type":"code","metadata":{"trusted":true,"id":"yqMD-5NGQjqG","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623859481415,"user_tz":420,"elapsed":160,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"f4f8574c-a504-44fa-e277-abf1d78550f0"},"source":["# variable_name = some_value\n","# is how we assign a value to a variable in Python\n","s = 'First line.\\nSecond line.'  # \\n means newline.\n","print(s)"],"execution_count":16,"outputs":[{"output_type":"stream","text":["First line.\n","Second line.\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"U-YmUy0GQjqN"},"source":["String literals can span multiple lines and are delineated by single or double triple-quotes: `\"\"\"...\"\"\"` or `'''...'''`. End of lines are automatically included in the string."]},{"cell_type":"code","metadata":{"trusted":true,"id":"B9UUMqnaQjqN","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623859506980,"user_tz":420,"elapsed":167,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"12e48427-9f4b-4960-a1df-4d4c466bf7a5"},"source":["print(\"\"\"\n","Do you enjoy learning Python?\n","    > Yes!\n","    > I'm trying!\n","\"\"\")"],"execution_count":17,"outputs":[{"output_type":"stream","text":["\n","Do you enjoy learning Python?\n","    > Yes!\n","    > I'm trying!\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"itnKnSw1QjqU"},"source":["Strings can be *concatenated* (glued together) with the `+` operator, and repeated with `*`:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"m-gZYAgpQjqU","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859525422,"user_tz":420,"elapsed":157,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"402feb74-908e-4ee8-e839-3ff0c6dd6d8c"},"source":["# 3 times 'Python', followed by 'RenPy'\n","3 * 'Python' + 'RenPy'"],"execution_count":18,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'PythonPythonPythonRenPy'"]},"metadata":{"tags":[]},"execution_count":18}]},{"cell_type":"markdown","metadata":{"id":"uQY-HK0cU7Ax"},"source":["Strings can be *indexed* (subscripted), with the first character at index 0."]},{"cell_type":"code","metadata":{"trusted":true,"id":"SZzpTFifQjqj","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859550799,"user_tz":420,"elapsed":228,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"fdc75bfd-3559-4214-d587-beca3c027214"},"source":["word = 'Python'\n","word[0]  # Character in position 0."],"execution_count":19,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'P'"]},"metadata":{"tags":[]},"execution_count":19}]},{"cell_type":"code","metadata":{"trusted":true,"id":"ayd2jTyTQjqk","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859552791,"user_tz":420,"elapsed":184,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"275247fd-58e0-4ff1-8ea6-e9733bdd1c93"},"source":["word[5]  # Character in position 5."],"execution_count":20,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'n'"]},"metadata":{"tags":[]},"execution_count":20}]},{"cell_type":"markdown","metadata":{"id":"f7-pXivuQjqn"},"source":["Indices may also be negative numbers, which means that we start counting from the end of the string. Note that because -0 is the same as 0, negative indices start from -1:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"Zk8EHjJyQjqo","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859585054,"user_tz":420,"elapsed":217,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"4c74caf7-29bb-4b83-c189-d6e1ab3628f4"},"source":["word = 'Python'\n","word[-1]  # Last character."],"execution_count":21,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'n'"]},"metadata":{"tags":[]},"execution_count":21}]},{"cell_type":"code","metadata":{"trusted":true,"id":"8ZbgslxBQjqp","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859588984,"user_tz":420,"elapsed":186,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"6c887d54-2311-4486-86f7-13a97d2902a9"},"source":["word[-2]  # Second-last character."],"execution_count":22,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'o'"]},"metadata":{"tags":[]},"execution_count":22}]},{"cell_type":"code","metadata":{"trusted":true,"id":"rIeDJMCzQjqq","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859591690,"user_tz":420,"elapsed":185,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"76b740f1-e98e-43ed-fb81-72b7474398db"},"source":["word[-6]"],"execution_count":23,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'P'"]},"metadata":{"tags":[]},"execution_count":23}]},{"cell_type":"markdown","metadata":{"id":"Kf9l379MQjqr"},"source":["In addition to indexing, which extracts individual characters, Python also supports *slicing*, which extracts a substring. To slice, you indicate a *range* in the format `start:end`, where the start position is included but the end position is excluded:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"wjA5zYeKQjqs","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859621397,"user_tz":420,"elapsed":201,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"3a9ec322-fcf2-4678-883a-af20a24e4aae"},"source":["word[0:2]  # Characters from position 0 (included) to 2 (excluded)."],"execution_count":24,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Py'"]},"metadata":{"tags":[]},"execution_count":24}]},{"cell_type":"code","metadata":{"trusted":true,"id":"qCMdCvJAQjqt","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859628073,"user_tz":420,"elapsed":227,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"c96499f1-cd15-43dc-c02e-a2033d22aa87"},"source":["word[2:5]  # Characters from position 2 (included) to 5 (excluded)."],"execution_count":25,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'tho'"]},"metadata":{"tags":[]},"execution_count":25}]},{"cell_type":"markdown","metadata":{"id":"NF15loCvQjqv"},"source":["If you omit either position, the default start position is 0 and the default end is the length of the string:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"KmhtaAiHQjqw","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859642663,"user_tz":420,"elapsed":214,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"056375ec-4c1c-4d34-9e4a-8a2e4eb6da70"},"source":["word[:2]   # Character from the beginning to position 2 (excluded)."],"execution_count":26,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Py'"]},"metadata":{"tags":[]},"execution_count":26}]},{"cell_type":"code","metadata":{"trusted":true,"id":"HvSviEApQjqx","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859644571,"user_tz":420,"elapsed":511,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"1495e784-ebea-48e3-dec8-832d0cf52a52"},"source":["word[4:]  # Characters from position 4 (included) to the end."],"execution_count":27,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'on'"]},"metadata":{"tags":[]},"execution_count":27}]},{"cell_type":"code","metadata":{"trusted":true,"id":"tx3VCcJSQjqy","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623859646253,"user_tz":420,"elapsed":168,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"ff046c72-0878-4b97-894f-979e4d52471d"},"source":["word[-2:] # Characters from the second-last (included) to the end."],"execution_count":28,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'on'"]},"metadata":{"tags":[]},"execution_count":28}]},{"cell_type":"markdown","metadata":{"id":"4cElbyUMQjq8"},"source":["Python strings are [immutable](https://docs.python.org/3.5/glossary.html#term-immutable), which means they cannot be changed. Therefore, assigning a letter to an indexed position in a string results in an error:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"D6MOeBkiQjq9","colab":{"base_uri":"https://localhost:8080/","height":190},"executionInfo":{"status":"error","timestamp":1623859670203,"user_tz":420,"elapsed":183,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"dea37091-6f90-4762-a978-b3310d492616"},"source":["word = 'Python'\n","word[0] = 'J'"],"execution_count":29,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)","\u001b[0;32m<ipython-input-29-ad89e228b316>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mword\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Python'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mword\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"EWf5GP2whngF","executionInfo":{"status":"ok","timestamp":1623859680229,"user_tz":420,"elapsed":168,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"57c92cce-93d6-4450-a5d1-e42f8f636786"},"source":["# you need to define a new string if you want one\n","new_word = 'J' + word[1:]\n","new_word"],"execution_count":30,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Jython'"]},"metadata":{"tags":[]},"execution_count":30}]},{"cell_type":"markdown","metadata":{"id":"n4TgAf1RQjrF"},"source":["The built-in function [`len()`](https://docs.python.org/3.5/library/functions.html#len) returns the length of a string:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"ZpPG_a8hQjrF","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623859710984,"user_tz":420,"elapsed":147,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"d6bbc648-2872-4d33-ba5a-67bafebb23ab"},"source":["s = 'Learning Python for RenPy is so cool'\n","len(s)"],"execution_count":31,"outputs":[{"output_type":"execute_result","data":{"text/plain":["36"]},"metadata":{"tags":[]},"execution_count":31}]},{"cell_type":"markdown","metadata":{"id":"3-VeggvI_a7c"},"source":["There are also functions that capitalize a string or transform it into upper or lowercase"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"eHWq9ADE_j5Y","executionInfo":{"status":"ok","timestamp":1623859732581,"user_tz":420,"elapsed":160,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"2318050f-8794-428f-e9ec-19fcd01e52ab"},"source":["s = 'python'\n","s.capitalize()"],"execution_count":32,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Python'"]},"metadata":{"tags":[]},"execution_count":32}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"tBzMc09x_n48","executionInfo":{"status":"ok","timestamp":1623859736647,"user_tz":420,"elapsed":204,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"0b394a05-64ac-4bd7-b6cb-99b0db709fc1"},"source":["s.upper()"],"execution_count":33,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'PYTHON'"]},"metadata":{"tags":[]},"execution_count":33}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"flD4yHUl_pIh","executionInfo":{"status":"ok","timestamp":1623859740511,"user_tz":420,"elapsed":179,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"e2e25f2e-4363-466b-fb5e-12b28fde984c"},"source":["s = 'RenPy'\n","s.lower()"],"execution_count":34,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'renpy'"]},"metadata":{"tags":[]},"execution_count":34}]},{"cell_type":"markdown","metadata":{"id":"VPM_K2jrQjpL"},"source":["## Numbers\n","- Integers\n","- Floats\n","\n","Syntax for doing arithmetic is straightforward: the operators `+`, `-`, `*` and `/`."]},{"cell_type":"code","metadata":{"trusted":true,"id":"32FQJB6dQjpM","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860638758,"user_tz":420,"elapsed":219,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"55804420-d3c3-4ee5-d0e0-e1f9343021b9"},"source":["2 + 1"],"execution_count":46,"outputs":[{"output_type":"execute_result","data":{"text/plain":["3"]},"metadata":{"tags":[]},"execution_count":46}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"t28NijvZDpTx","executionInfo":{"status":"ok","timestamp":1623860641633,"user_tz":420,"elapsed":183,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"30a1f022-572c-4ab9-9342-29ffb0e5c67a"},"source":["1 + 3 + 6 - 10"],"execution_count":47,"outputs":[{"output_type":"execute_result","data":{"text/plain":["0"]},"metadata":{"tags":[]},"execution_count":47}]},{"cell_type":"markdown","metadata":{"id":"4fjvqI9FnGkz"},"source":["Parentheses (`()`) can be used for grouping and ordering operations."]},{"cell_type":"code","metadata":{"trusted":true,"id":"tLeIbAcaQjpQ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860651443,"user_tz":420,"elapsed":179,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"3980e081-b116-45dd-c5ca-654fcb3892c9"},"source":["(50 - 5 * 6) / 4"],"execution_count":48,"outputs":[{"output_type":"execute_result","data":{"text/plain":["5.0"]},"metadata":{"tags":[]},"execution_count":48}]},{"cell_type":"markdown","metadata":{"id":"rt4MhKvqQjpT"},"source":["The integer numbers (e.g. `2`, `4`, `20`) have type [`int`](https://docs.python.org/3.5/library/functions.html#int), and the ones with a fractional part (e.g. `5.0`, `1.6`) have type [`float`](https://docs.python.org/3.5/library/functions.html#float).\n","\n","Division (`/`) always returns a float. To do [floor division](https://docs.python.org/3.5/glossary.html#term-floor-division) and get an integer result (discarding any fractional result) you can use the `//` operator; to calculate the remainder you can use `%`:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"OG3PR7OqQjpU","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860682502,"user_tz":420,"elapsed":229,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"22e01402-22ed-4fce-d423-553d31a465de"},"source":["17 / 3  # Classic division returns a float."],"execution_count":49,"outputs":[{"output_type":"execute_result","data":{"text/plain":["5.666666666666667"]},"metadata":{"tags":[]},"execution_count":49}]},{"cell_type":"code","metadata":{"trusted":true,"id":"mQLQYmHEQjpX","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860686194,"user_tz":420,"elapsed":203,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"e79de848-6bee-4349-c6f6-1f113c2b3af2"},"source":["17 // 3  # Floor division discards the fractional part."],"execution_count":50,"outputs":[{"output_type":"execute_result","data":{"text/plain":["5"]},"metadata":{"tags":[]},"execution_count":50}]},{"cell_type":"code","metadata":{"trusted":true,"id":"qkiKzO2MQjpZ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860689286,"user_tz":420,"elapsed":162,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"57ef2a17-f6d8-4d53-fc62-65c1c14d2149"},"source":["17 % 3  # mod: The % operator returns the remainder of the division."],"execution_count":51,"outputs":[{"output_type":"execute_result","data":{"text/plain":["2"]},"metadata":{"tags":[]},"execution_count":51}]},{"cell_type":"markdown","metadata":{"id":"S_GTPtbGQjpd"},"source":["Use the `**` operator to calculate powers:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"zx_zlJyWQjpg","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860705101,"user_tz":420,"elapsed":245,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"3fbb8f31-e2ff-4e21-affa-b91a25a560dc"},"source":["2 ** 7  # 2 to the power of 7"],"execution_count":52,"outputs":[{"output_type":"execute_result","data":{"text/plain":["128"]},"metadata":{"tags":[]},"execution_count":52}]},{"cell_type":"code","metadata":{"trusted":true,"id":"vVGly7_bQjpe","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623860707531,"user_tz":420,"elapsed":221,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"cecd232e-e068-4eb7-e24a-1d4537e69975"},"source":["5 ** 2  # 5 squared"],"execution_count":53,"outputs":[{"output_type":"execute_result","data":{"text/plain":["25"]},"metadata":{"tags":[]},"execution_count":53}]},{"cell_type":"markdown","metadata":{"id":"nGIP_JUSHbHL"},"source":["Take the maximum or minimum of several numbers using `max()` or `min()`"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"FFE87U1QHocD","executionInfo":{"status":"ok","timestamp":1623860728155,"user_tz":420,"elapsed":233,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"48cbfb44-204f-4982-9c39-d8d3294b8e3b"},"source":["max(100, 200, 500)"],"execution_count":54,"outputs":[{"output_type":"execute_result","data":{"text/plain":["500"]},"metadata":{"tags":[]},"execution_count":54}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_qq8cogRHqEb","executionInfo":{"status":"ok","timestamp":1623860732573,"user_tz":420,"elapsed":230,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"02c85d66-9cf5-4f4c-e34f-b0020c1dc124"},"source":["min(-20, -90)"],"execution_count":55,"outputs":[{"output_type":"execute_result","data":{"text/plain":["-90"]},"metadata":{"tags":[]},"execution_count":55}]},{"cell_type":"markdown","metadata":{"id":"xBRIg8sXXzIa"},"source":["## Booleans\n"]},{"cell_type":"markdown","metadata":{"id":"Wo0BWyQGIiG5"},"source":["Logical variables that hold `True` or `False` values"]},{"cell_type":"code","metadata":{"id":"Eds0lEArX0lb","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623861743345,"user_tz":420,"elapsed":170,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"e4108df3-a5bb-4ca6-9f27-1abfc2f30bc5"},"source":["is_player_awake = True\n","is_player_awake"],"execution_count":56,"outputs":[{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{"tags":[]},"execution_count":56}]},{"cell_type":"code","metadata":{"id":"2B7O3mRdX5Wp","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623861749801,"user_tz":420,"elapsed":171,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"0edb78c1-5495-4703-8c32-34be9ec5cef3"},"source":["not is_player_awake"],"execution_count":57,"outputs":[{"output_type":"execute_result","data":{"text/plain":["False"]},"metadata":{"tags":[]},"execution_count":57}]},{"cell_type":"code","metadata":{"id":"9GsQv9jIX70q","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623861765275,"user_tz":420,"elapsed":276,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"fa2dbcc1-c4e7-453b-e034-ed7086678430"},"source":["is_player_at_home = False\n","# are they both true?\n","# evaluates to `True and False` => False\n","is_player_awake and is_player_at_home"],"execution_count":58,"outputs":[{"output_type":"execute_result","data":{"text/plain":["False"]},"metadata":{"tags":[]},"execution_count":58}]},{"cell_type":"code","metadata":{"id":"AYUo6o82YAIz","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623861781349,"user_tz":420,"elapsed":250,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"5eab675b-336e-4020-fdf0-7b42f843d9e3"},"source":["# is either of them true?\n","# `True or False` => True\n","is_player_awake or is_player_at_home"],"execution_count":59,"outputs":[{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{"tags":[]},"execution_count":59}]},{"cell_type":"markdown","metadata":{"id":"i66-ZYDVQjrG"},"source":["## Lists\n"]},{"cell_type":"markdown","metadata":{"id":"OPiotZc4QjrH"},"source":["The most basic and versatile data structure is the [*list*](https://docs.python.org/3.5/library/stdtypes.html#typesseq-list), which can be written as a sequence of comma-separated items between square brackets. Lists might contain items of different types, but usually the items all have the same type."]},{"cell_type":"code","metadata":{"trusted":true,"id":"OdN8ywPHQjrH","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863529880,"user_tz":420,"elapsed":185,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"c2333f45-cbb4-4211-bcc4-e9f79de32865"},"source":["squares = [1, 4, 9, 16, 25]\n","squares"],"execution_count":64,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[1, 4, 9, 16, 25]"]},"metadata":{"tags":[]},"execution_count":64}]},{"cell_type":"markdown","metadata":{"id":"qmjXZ-AsQjrJ"},"source":["Like strings (and all other built-in [sequence](https://docs.python.org/3.5/glossary.html#term-sequence) types), lists can be indexed and sliced:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"lQ9TdZWrQjrJ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863540271,"user_tz":420,"elapsed":166,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"21f5108f-e79b-48b6-975e-b3e25aa6fc72"},"source":["squares[0]  # Indexing returns the item."],"execution_count":65,"outputs":[{"output_type":"execute_result","data":{"text/plain":["1"]},"metadata":{"tags":[]},"execution_count":65}]},{"cell_type":"code","metadata":{"trusted":true,"id":"Tv4FsEHRQjrK","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863547200,"user_tz":420,"elapsed":175,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"e8db47f3-83fb-40d7-b393-d2df6323b18e"},"source":["squares[-1]"],"execution_count":66,"outputs":[{"output_type":"execute_result","data":{"text/plain":["25"]},"metadata":{"tags":[]},"execution_count":66}]},{"cell_type":"code","metadata":{"trusted":true,"id":"HZI0i6ixQjrM","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863551265,"user_tz":420,"elapsed":162,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"6a804e5e-1bbf-41b6-d771-21e20d767ba1"},"source":["squares[-3:]  # Slicing returns a new list."],"execution_count":67,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[9, 16, 25]"]},"metadata":{"tags":[]},"execution_count":67}]},{"cell_type":"markdown","metadata":{"id":"P9W1AGy-QjrR"},"source":["Unlike strings, which are [immutable](https://docs.python.org/3.5/glossary.html#term-immutable), lists are a [mutable](https://docs.python.org/3.5/glossary.html#term-mutable) type, which means you can change any value in the list:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"k-bUMdEDQjrS","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863585068,"user_tz":420,"elapsed":167,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"1643ea45-f6b1-40d6-f4a0-6d2151181c6c"},"source":["cubes = [1, 8, 27, 65, 125]  # Something's wrong here ...\n","4 ** 3  # the cube of 4 is 64, not 65!"],"execution_count":68,"outputs":[{"output_type":"execute_result","data":{"text/plain":["64"]},"metadata":{"tags":[]},"execution_count":68}]},{"cell_type":"code","metadata":{"trusted":true,"id":"2UmKosrgQjrT","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863608864,"user_tz":420,"elapsed":150,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"ad59de4c-4196-4ee1-a587-5fa0fcab653c"},"source":["cubes[3] = 64  # Replace the wrong value.\n","cubes"],"execution_count":69,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[1, 8, 27, 64, 125]"]},"metadata":{"tags":[]},"execution_count":69}]},{"cell_type":"markdown","metadata":{"id":"6-JeM4EHQjrU"},"source":["Use the list's `append()` method to add new items to the end of the list:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"GaVZnYdpQjrU","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863647558,"user_tz":420,"elapsed":226,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"37c20f85-257a-4758-ca8f-9b275c3c21ba"},"source":["cubes.append(216)  # Add the cube of 6 ...\n","cubes.append(7 ** 3)  # and the cube of 7.\n","cubes"],"execution_count":70,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[1, 8, 27, 64, 125, 216, 343]"]},"metadata":{"tags":[]},"execution_count":70}]},{"cell_type":"markdown","metadata":{"id":"uWy85fK0Qjra"},"source":["The built-in [`len()`](https://docs.python.org/3.5/library/functions.html#len) function also applies to lists:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"op9idL8bQjrc","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863664684,"user_tz":420,"elapsed":181,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"f13603b9-b241-482c-9eaf-c205ccffd6ac"},"source":["letters = ['a', 'b', 'c', 'd']\n","len(letters)"],"execution_count":71,"outputs":[{"output_type":"execute_result","data":{"text/plain":["4"]},"metadata":{"tags":[]},"execution_count":71}]},{"cell_type":"markdown","metadata":{"id":"AS1fZKo5NO4s"},"source":["Common mistake: list index starts at 0 so the last element is at position `len(list) - 1`, not `len(list)`"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":171},"id":"WymSV-I7NjIs","executionInfo":{"status":"error","timestamp":1623863688214,"user_tz":420,"elapsed":175,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"39036f04-348c-4d8a-acb5-2e1b63cc92b6"},"source":["letters[len(letters)] # This throws an error"],"execution_count":72,"outputs":[{"output_type":"error","ename":"IndexError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)","\u001b[0;32m<ipython-input-72-be4f03d6b50d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mletters\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# This throws an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mIndexError\u001b[0m: list index out of range"]}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"hPq4eFYzNmyh","executionInfo":{"status":"ok","timestamp":1623863701491,"user_tz":420,"elapsed":199,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"8bd6581e-0a14-473d-9e57-053c4d6d6809"},"source":["# the correct way to retrieve the last character\n","letters[len(letters) - 1]"],"execution_count":73,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'d'"]},"metadata":{"tags":[]},"execution_count":73}]},{"cell_type":"markdown","metadata":{"id":"9V9LsgUAQjrd"},"source":["Lists can have duplicate elements. You can aslo nest lists, which means to create lists that contain other lists. For example:"]},{"cell_type":"code","metadata":{"trusted":true,"id":"0MsX4HOGQjre","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863744321,"user_tz":420,"elapsed":202,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"0bbfd582-68fa-4920-e678-992f64082699"},"source":["fruits = ['apple', 'banana', 'pear', 'apple']\n","veggies = ['carrot', 'cabbage']\n","ingredients = [fruits, veggies]\n","ingredients"],"execution_count":74,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[['apple', 'banana', 'pear', 'apple'], ['carrot', 'cabbage']]"]},"metadata":{"tags":[]},"execution_count":74}]},{"cell_type":"code","metadata":{"trusted":true,"id":"sKiPL6reQjrh","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623863753851,"user_tz":420,"elapsed":228,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"cdb45c8d-4e33-4262-880a-ccdd4194fc39"},"source":["ingredients[0]"],"execution_count":75,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['apple', 'banana', 'pear', 'apple']"]},"metadata":{"tags":[]},"execution_count":75}]},{"cell_type":"code","metadata":{"trusted":true,"id":"-w89aYm-Qjri","colab":{"base_uri":"https://localhost:8080/","height":37},"executionInfo":{"status":"ok","timestamp":1623863758773,"user_tz":420,"elapsed":228,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"6f2937a9-9e95-4e3c-883e-2ea8d387a45d"},"source":["ingredients[0][1]"],"execution_count":76,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'banana'"]},"metadata":{"tags":[]},"execution_count":76}]},{"cell_type":"markdown","metadata":{"id":"SJ3d92h2JOkG"},"source":["## Sets\n","\n","Crucial differences from lists:\n","\n","- Lists are ordered and sets are not\n","- Lists can contain duplicate elements and sets cannot\n","\n","Sets are used for checking membership, i.e., whether an item is contained in a set. You can do the same membership-checking with lists, but using sets are more time-efficient.\n","\n","To put simply, looking up an item in a list requires you to walk through the list, while looking up an item in a set is constant time"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"xHWAtfpEQvzc","executionInfo":{"status":"ok","timestamp":1623864765808,"user_tz":420,"elapsed":170,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"6bb21089-006b-4910-a227-5e83139e3900"},"source":["item_list = ['apple', 'banana', 'pear', 'apple']\n","item_list"],"execution_count":77,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['apple', 'banana', 'pear', 'apple']"]},"metadata":{"tags":[]},"execution_count":77}]},{"cell_type":"markdown","metadata":{"id":"6v17MNE92O14"},"source":["Define set using `set([...])`"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"XIvypRFwRHSH","executionInfo":{"status":"ok","timestamp":1623864775950,"user_tz":420,"elapsed":160,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"caecc011-ae59-4630-d026-643a72f144e4"},"source":["item_set = set(['apple', 'banana', 'pear', 'apple'])\n","item_set # no duplicates"],"execution_count":78,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'apple', 'banana', 'pear'}"]},"metadata":{"tags":[]},"execution_count":78}]},{"cell_type":"markdown","metadata":{"id":"LW7FAtMx2bGr"},"source":["Check membership using the `in` keyword"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"xU1DG-_URJZO","executionInfo":{"status":"ok","timestamp":1623864800537,"user_tz":420,"elapsed":225,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"a53a8fa1-31ce-4cf2-9bcc-612f98aecdd2"},"source":["# check membership\n","'apple' in item_set"],"execution_count":79,"outputs":[{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{"tags":[]},"execution_count":79}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oEcsKp7MRNQf","executionInfo":{"status":"ok","timestamp":1623864806852,"user_tz":420,"elapsed":178,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"69547f2f-ca0d-4a13-f9a3-d3d255cfbcef"},"source":["'apple' in item_list"],"execution_count":80,"outputs":[{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{"tags":[]},"execution_count":80}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"AUNrfZbQROzf","executionInfo":{"status":"ok","timestamp":1623864810965,"user_tz":420,"elapsed":188,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"af9307bb-855b-4b36-894a-9ddd8128a23e"},"source":["'peach' in item_set"],"execution_count":81,"outputs":[{"output_type":"execute_result","data":{"text/plain":["False"]},"metadata":{"tags":[]},"execution_count":81}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"4uRxSQelRgAT","executionInfo":{"status":"ok","timestamp":1623864835634,"user_tz":420,"elapsed":189,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"5ae46339-552e-45c9-ed3d-30f98397c5fe"},"source":["# add to a set\n","item_set.add('starfruit')\n","item_set"],"execution_count":82,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'apple', 'banana', 'pear', 'starfruit'}"]},"metadata":{"tags":[]},"execution_count":82}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8DLTWH6WRivX","executionInfo":{"status":"ok","timestamp":1623864852015,"user_tz":420,"elapsed":207,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"83db6311-bd39-4f2c-b262-468c72d88ac0"},"source":["# duplicate elements are ignored when adding\n","item_set.add('apple')\n","item_set"],"execution_count":83,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'apple', 'banana', 'pear', 'starfruit'}"]},"metadata":{"tags":[]},"execution_count":83}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IsofRiUWRuWp","executionInfo":{"status":"ok","timestamp":1623864864165,"user_tz":420,"elapsed":196,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"1437c3e3-a59f-4d32-dbab-2b032c0a7945"},"source":["# recall that list append tolerates duplicates\n","item_list"],"execution_count":84,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['apple', 'banana', 'pear', 'apple']"]},"metadata":{"tags":[]},"execution_count":84}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"YnmWYkF-R0pv","executionInfo":{"status":"ok","timestamp":1623864870363,"user_tz":420,"elapsed":200,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"98fdffab-2dd1-4cb4-e58a-5644a81fe424"},"source":["item_list.append('apple')\n","item_list"],"execution_count":85,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['apple', 'banana', 'pear', 'apple', 'apple']"]},"metadata":{"tags":[]},"execution_count":85}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"AIa9gPwmSJ9f","executionInfo":{"status":"ok","timestamp":1623864895543,"user_tz":420,"elapsed":175,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"abba5f17-9493-4a2a-f8ec-776b54459f90"},"source":["# can also use len() to get the number of elements\n","len(item_set)"],"execution_count":86,"outputs":[{"output_type":"execute_result","data":{"text/plain":["4"]},"metadata":{"tags":[]},"execution_count":86}]},{"cell_type":"markdown","metadata":{"id":"RDlIm1PVVYcN"},"source":["## Dictionaries\n","\n","Dictionaries store key-value pairs. Just like sets, looking up a key is constant time.\n","\n","Syntax:\n","\n","```python\n","dictionary = { key: value }\n","```"]},{"cell_type":"code","metadata":{"id":"1A0Fmp4WVixV","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623865820209,"user_tz":420,"elapsed":257,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"4a6d941b-205d-485f-dc69-6c6f2b9841c0"},"source":["# maps letter grade to numeric scores\n","grades = {'A': [90, 100], \n","          'B': [80, 89], \n","          'C': [70, 79], \n","          'D': [60, 69]\n","          }\n","grades"],"execution_count":87,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'A': [90, 100], 'B': [80, 89], 'C': [70, 79], 'D': [60, 69]}"]},"metadata":{"tags":[]},"execution_count":87}]},{"cell_type":"markdown","metadata":{"id":"SQyFZvIG64Jy"},"source":["Look up using `dict[key]`"]},{"cell_type":"code","metadata":{"id":"xNxce-uBV7Qr","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623865865011,"user_tz":420,"elapsed":144,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"92967476-bf28-469a-d032-5c088f06b17d"},"source":["grades['D']"],"execution_count":90,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[60, 69]"]},"metadata":{"tags":[]},"execution_count":90}]},{"cell_type":"code","metadata":{"id":"J9u5UwIRV-Uo","colab":{"base_uri":"https://localhost:8080/","height":171},"executionInfo":{"status":"error","timestamp":1623865896394,"user_tz":420,"elapsed":164,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"016e453f-3eb6-4de8-9625-3c54344bc20d"},"source":["grades['F'] # there is no key named 'F' and thus this throws an error"],"execution_count":93,"outputs":[{"output_type":"error","ename":"KeyError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)","\u001b[0;32m<ipython-input-93-3de8ba53de99>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgrades\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'F'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# there is no key named 'F' and thus this throws an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mKeyError\u001b[0m: 'F'"]}]},{"cell_type":"code","metadata":{"id":"4aMrFeWDWa9z","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623865922427,"user_tz":420,"elapsed":183,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"d7008a3d-ae63-4185-8c12-753dc11b08de"},"source":["grades['F'] = [50, 59] # we can add new key-value pairs like this\n","grades"],"execution_count":94,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'A': [90, 100], 'B': [80, 89], 'C': [70, 79], 'D': [60, 69], 'F': [50, 59]}"]},"metadata":{"tags":[]},"execution_count":94}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"W0SZqlhV87D8","executionInfo":{"status":"ok","timestamp":1623865935109,"user_tz":420,"elapsed":164,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"5ddd60a8-a385-47a8-edc3-7ec2a67b8562"},"source":["grades['F']"],"execution_count":95,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[50, 59]"]},"metadata":{"tags":[]},"execution_count":95}]},{"cell_type":"code","metadata":{"id":"l9K-OofrWj8D","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623865947548,"user_tz":420,"elapsed":316,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"c7a15a99-5c33-4027-d215-8f9d9089ce52"},"source":["grades['F'] = [0, 59] # we can overwrite a key-value pair\n","grades"],"execution_count":96,"outputs":[{"output_type":"execute_result","data":{"text/plain":["{'A': [90, 100], 'B': [80, 89], 'C': [70, 79], 'D': [60, 69], 'F': [0, 59]}"]},"metadata":{"tags":[]},"execution_count":96}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"BejZoaEu9BOH","executionInfo":{"status":"ok","timestamp":1623865960247,"user_tz":420,"elapsed":14,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"3b9ffff3-08f5-4fb2-b7ca-33a60e54dced"},"source":["grades['F']"],"execution_count":97,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[0, 59]"]},"metadata":{"tags":[]},"execution_count":97}]},{"cell_type":"markdown","metadata":{"id":"4DWBzytZWqGs"},"source":["## Conditional Statements\n"]},{"cell_type":"markdown","metadata":{"id":"jHh4sxwdU9QM"},"source":["Use `if` and `else` with colon `:` and proper indentation. `if` should be followed by a statement that evaluates to either `True` or `False`. You may want to review the section on Booleans"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"AYaQTiCyVfXa","executionInfo":{"status":"ok","timestamp":1623870556796,"user_tz":420,"elapsed":164,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"f87ab9dc-58c0-4068-d860-385c938c0900"},"source":["if True:\n","  print('Hello!')"],"execution_count":106,"outputs":[{"output_type":"stream","text":["Hello!\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"Nk2MxucgKnYu","executionInfo":{"status":"ok","timestamp":1623870572327,"user_tz":420,"elapsed":167,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["# nothing is printed\n","if False:\n","  print('Hello!')"],"execution_count":107,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"XbHIboMgVuqb","executionInfo":{"status":"ok","timestamp":1623870593827,"user_tz":420,"elapsed":224,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"818d7b1e-dc11-4ad9-91e8-fae7d263ae17"},"source":["2 < 3 # this is a True statement"],"execution_count":108,"outputs":[{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{"tags":[]},"execution_count":108}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oX_3X4y9VqtT","executionInfo":{"status":"ok","timestamp":1623870607240,"user_tz":420,"elapsed":171,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"05429025-84ac-497b-983f-04d6577516c3"},"source":["if 2 < 3:\n","  print('Hello!')"],"execution_count":109,"outputs":[{"output_type":"stream","text":["Hello!\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"moV-PB-PK9Lf","executionInfo":{"status":"ok","timestamp":1623870617657,"user_tz":420,"elapsed":145,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"1a990f76-2c41-4f57-d4e4-a65b6d539ff9"},"source":["2 > 3"],"execution_count":110,"outputs":[{"output_type":"execute_result","data":{"text/plain":["False"]},"metadata":{"tags":[]},"execution_count":110}]},{"cell_type":"code","metadata":{"id":"ziQ8Om3SLAc8","executionInfo":{"status":"ok","timestamp":1623870636868,"user_tz":420,"elapsed":4,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["# nothing is printed\n","if 2 > 3:\n","  print('Hello!')"],"execution_count":112,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Cb_ah-JuLSjZ","executionInfo":{"status":"ok","timestamp":1623870668016,"user_tz":420,"elapsed":263,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"d9bee1eb-3854-4fd7-f93c-f8d35683c6f7"},"source":["# use `if` without `else`\n","score = 94\n","if 90 <= score and score <= 100:\n","  print(\"You got an A!\")"],"execution_count":113,"outputs":[{"output_type":"stream","text":["You got an A!\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Kjlx4oznVMJM","executionInfo":{"status":"ok","timestamp":1623870722267,"user_tz":420,"elapsed":168,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"5c6db09d-c5ec-45d4-8488-b6022c3cf69f"},"source":["# can also directly chain together arithemetic comparison operations\n","if 90 <= score <= 100:\n","  print(\"You got an A!\")"],"execution_count":114,"outputs":[{"output_type":"stream","text":["You got an A!\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"lKl93rQ4Lsp2"},"source":["Syntax for `if-else`, `else` catches all other cases where `some_condition` evaluates to `False`\n","\n","```python\n","if some_condition:\n","  # do something\n","else:\n","  # do something\n","```"]},{"cell_type":"code","metadata":{"id":"8KOvKqznWy8v","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623870756375,"user_tz":420,"elapsed":146,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"0590e9b1-d377-4409-e929-888e88df6a0b"},"source":["# use `if` with `else`\n","score = 94\n","if 90 <= score <= 100:\n","  print(\"You got an A!\")\n","else:\n","  print(\"You got some other grades.\")"],"execution_count":115,"outputs":[{"output_type":"stream","text":["You got an A!\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"brS47nJaU5my","executionInfo":{"status":"ok","timestamp":1623870767800,"user_tz":420,"elapsed":145,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"8a597229-df47-4028-b1ec-1401f4eb670e"},"source":["# change the score to fail the if condition\n","score = 88\n","if 90 <= score <= 100:\n","  print(\"You got an A!\")\n","else:\n","  print(\"You got some other grades.\")"],"execution_count":116,"outputs":[{"output_type":"stream","text":["You got some other grades.\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Cs-eY5HIVDO0"},"source":["Use `elif` (else if) to check for more conditions before falling through to `else`"]},{"cell_type":"code","metadata":{"id":"XKw2ul8PXTeF","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623870814575,"user_tz":420,"elapsed":201,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"4029bb6d-b2b9-40ce-db4a-4b3e4209dcca"},"source":["score = 88\n","if 90 <= score <= 100:\n","  print(\"You got an A!\")\n","elif 80 <= score <= 89:\n","  print(\"You got an B. Try harder next time!\")\n","else:\n","  print(\"You got some other grades.\")"],"execution_count":119,"outputs":[{"output_type":"stream","text":["You got an B. Try harder next time!\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"40NwTDl3XswK"},"source":["## Loops\n","\n","Loops save us efforts from doing repeated work. We will cover `for` and `while` loops."]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dBfwlesJQADT","executionInfo":{"status":"ok","timestamp":1623872174028,"user_tz":420,"elapsed":254,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"495257b9-1cac-45ab-ef4e-a264f6a9ec97"},"source":["# say if I want to count from 0 to 9\n","# I can of course do\n","print(\"Hello from 0\")\n","print(\"Hello from 1\")\n","# ...\n","print(\"Hello from 9\")"],"execution_count":121,"outputs":[{"output_type":"stream","text":["Hello from 0\n","Hello from 1\n","Hello from 9\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"sRQt_ljsRvx3"},"source":["Syntax\n","\n","```python\n","for variable_name in range(...):\n","  # do something\n","```"]},{"cell_type":"code","metadata":{"id":"NTir__iBYQ6b","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623872203522,"user_tz":420,"elapsed":172,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"82e22b60-9889-40a3-84a1-e12c72293023"},"source":["# note that this excludes the upper value 10\n","for i in range(10):\n","  print(\"Hello from\", i)"],"execution_count":122,"outputs":[{"output_type":"stream","text":["Hello from 0\n","Hello from 1\n","Hello from 2\n","Hello from 3\n","Hello from 4\n","Hello from 5\n","Hello from 6\n","Hello from 7\n","Hello from 8\n","Hello from 9\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"uEQ8NnHhZcM7","executionInfo":{"status":"ok","timestamp":1623872239326,"user_tz":420,"elapsed":144,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"118bccf2-123d-496f-d023-925604f6a27f"},"source":["# what is range() really?\n","list(range(10))"],"execution_count":124,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"]},"metadata":{"tags":[]},"execution_count":124}]},{"cell_type":"markdown","metadata":{"id":"wrZTpbA2ZjGq"},"source":["We can also count backwards"]},{"cell_type":"code","metadata":{"id":"odCNaDQcYnkC","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623872261167,"user_tz":420,"elapsed":161,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"83710be8-2680-4421-d468-533542232c16"},"source":["# range(9, -1, -1) means\n","# start from 9, end before -1, increment by -1 each time\n","for i in range(9, -1, -1):\n","  print(\"Hello from\", i)"],"execution_count":125,"outputs":[{"output_type":"stream","text":["Hello from 9\n","Hello from 8\n","Hello from 7\n","Hello from 6\n","Hello from 5\n","Hello from 4\n","Hello from 3\n","Hello from 2\n","Hello from 1\n","Hello from 0\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Li6LU7VDZ83-"},"source":["While loops are another kind of loops\n","\n","Syntax\n","\n","```python\n","while some_condition:\n","  # do something\n","```"]},{"cell_type":"code","metadata":{"id":"Z61fEch-YYry","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623872320365,"user_tz":420,"elapsed":158,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"25b40cd1-defe-4c89-8d0c-c1170fae0b74"},"source":["i = 0 # initalize\n","while i < 10:\n","  print(\"Hello from\", i)\n","  i += 1 # increment"],"execution_count":126,"outputs":[{"output_type":"stream","text":["Hello from 0\n","Hello from 1\n","Hello from 2\n","Hello from 3\n","Hello from 4\n","Hello from 5\n","Hello from 6\n","Hello from 7\n","Hello from 8\n","Hello from 9\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"tRL9KN8BS9u_","executionInfo":{"status":"ok","timestamp":1623872339643,"user_tz":420,"elapsed":166,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"90d98d36-66d8-4252-c9d9-b4696c7ffb05"},"source":["# the cell above is equivalent to our for loop from before\n","for i in range(10):\n","  print(\"Hello from\", i)"],"execution_count":127,"outputs":[{"output_type":"stream","text":["Hello from 0\n","Hello from 1\n","Hello from 2\n","Hello from 3\n","Hello from 4\n","Hello from 5\n","Hello from 6\n","Hello from 7\n","Hello from 8\n","Hello from 9\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"dz766oLyYfb_","executionInfo":{"status":"ok","timestamp":1623872407999,"user_tz":420,"elapsed":196,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["# be careful of infinite loops!\n","# uncomment and run the code below\n","# i = 0\n","# while i < 10:\n","#   print(\"Hello from\", i)\n","#   # no change to i here\n","\n","# while True:\n","#   print(\"...\")"],"execution_count":131,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"IMRHc91WY6CN"},"source":["## Looping over Data Structures\n","\n","Iterate over a data structure like a list, a set, or a dict\n","\n","Recall the syntax for the `for` loop\n","\n","```python\n","for variable in iterable:\n","  # do something\n","```\n","\n"]},{"cell_type":"code","metadata":{"id":"tvyK6_jwY8_u","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623873580343,"user_tz":420,"elapsed":220,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"a54bae3f-3d7c-44b6-f1d9-f83ed522a2b1"},"source":["# iterate over a list\n","fruits = ['apple', 'banana', 'pear', 'orange']\n","for fruit in fruits:\n","  print('A yummy', fruit)"],"execution_count":132,"outputs":[{"output_type":"stream","text":["A yummy apple\n","A yummy banana\n","A yummy pear\n","A yummy orange\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"AIQdCSP_ccCY","executionInfo":{"status":"ok","timestamp":1623873610514,"user_tz":420,"elapsed":167,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"770e6fdb-eeb4-49a1-ad44-4977ede8e7fb"},"source":["# iterate over a set\n","# pay attention to how the items are ordered arbitrarily\n","fruit_set = set(['apple', 'banana', 'pear', 'orange'])\n","for fruit in fruit_set:\n","  print('A yummy', fruit)"],"execution_count":135,"outputs":[{"output_type":"stream","text":["A yummy apple\n","A yummy banana\n","A yummy pear\n","A yummy orange\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"3m5VUdC3ZHHj","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623873687315,"user_tz":420,"elapsed":156,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"fb213241-09e6-4161-f119-918b4f8d093a"},"source":["# iterate over the keys in a dictionary\n","fruit_colors = {'apple': 'red', \n","                'banana': 'yellow', \n","                'pear': 'green', \n","                'orange': 'orange',\n","                'peach': 'pink'}\n","for fruit in fruit_colors:\n","  color = fruit_colors[fruit] # look up the fruit key\n","  print('A yummy', color, fruit)"],"execution_count":137,"outputs":[{"output_type":"stream","text":["A yummy red apple\n","A yummy yellow banana\n","A yummy green pear\n","A yummy orange orange\n","A yummy pink peach\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"pi8MpCxnYH2z"},"source":["## Functions\n","Functions are versatile and save us from copy-pasting repeated blocks of code. Think of them as routines or recipes.\n"]},{"cell_type":"code","metadata":{"id":"EXmExWa4a0oa","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623875360284,"user_tz":420,"elapsed":171,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"dc98e41f-4ae0-4779-f86b-bac2ee1dd2b4"},"source":["# suppose I want to make an apple smoothie\n","fruit = 'apple'\n","print('Let\\'s make a ' + fruit + ' smoothie!')\n","print('Peeling ' + fruit + '...')\n","print('Blending ' + fruit + ' in a blender...')\n","print('Adding milk...')\n","print('Enjoy a delicious ' + fruit + ' smoothie!\\n\\n')"],"execution_count":141,"outputs":[{"output_type":"stream","text":["Let's make a apple smoothie!\n","Peeling apple...\n","Blending apple in a blender...\n","Adding milk...\n","Enjoy a delicious apple smoothie!\n","\n","\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"7YAQT80ZfybJ","executionInfo":{"status":"ok","timestamp":1623875370386,"user_tz":420,"elapsed":165,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"e7f4c74c-4854-4b95-fabd-3eacc3f678e3"},"source":["# now if I want to make a banana smoothie\n","# I need to copy-paste a lot of code\n","# which is not efficient\n","fruit = 'banana'\n","print('Let\\'s make a ' + fruit + ' smoothie!')\n","print('Peeling ' + fruit + '...')\n","print('Blending ' + fruit + 'in a blender...')\n","print('Adding milk...')\n","print('Enjoy a delicious ' + fruit + ' smoothie!\\n\\n')"],"execution_count":142,"outputs":[{"output_type":"stream","text":["Let's make a banana smoothie!\n","Peeling banana...\n","Blending bananain a blender...\n","Adding milk...\n","Enjoy a delicious banana smoothie!\n","\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Pr3uMiPWf_Om"},"source":["Instead, let's leverage the power of functions.\n","\n","Function definitions look like this:\n","\n","```python\n","def function_name(parameter1, parameter2, ...):\n","  # do something\n","  return some_value\n","```"]},{"cell_type":"code","metadata":{"id":"nn6VBBCGaVKZ","executionInfo":{"status":"ok","timestamp":1623875409060,"user_tz":420,"elapsed":173,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["def make_smoothie(fruit):\n","  print('Let\\'s make a ' + fruit + ' smoothie!')\n","  print('Peeling ' + fruit + '...')\n","  print('Blending ' + fruit + 'in a blender...')\n","  print('Adding milk...')\n","  print('Enjoy a delicious ' + fruit + ' smoothie!\\n\\n')"],"execution_count":143,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1hhrVHETd3BL","executionInfo":{"status":"ok","timestamp":1623875423115,"user_tz":420,"elapsed":176,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"74302aff-6fbc-426a-e01b-93f6d373f620"},"source":["make_smoothie('pineapple')"],"execution_count":144,"outputs":[{"output_type":"stream","text":["Let's make a pineapple smoothie!\n","Peeling pineapple...\n","Blending pineapplein a blender...\n","Adding milk...\n","Enjoy a delicious pineapple smoothie!\n","\n","\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"Z5avM8aLZpNJ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1623875447191,"user_tz":420,"elapsed":248,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"a63ca3f2-25b4-4eb0-c94c-b796e5f9bd5a"},"source":["for fruit in ['apple', 'banana', 'dragonfruit']:\n","  make_smoothie(fruit)"],"execution_count":145,"outputs":[{"output_type":"stream","text":["Let's make a apple smoothie!\n","Peeling apple...\n","Blending applein a blender...\n","Adding milk...\n","Enjoy a delicious apple smoothie!\n","\n","\n","Let's make a banana smoothie!\n","Peeling banana...\n","Blending bananain a blender...\n","Adding milk...\n","Enjoy a delicious banana smoothie!\n","\n","\n","Let's make a dragonfruit smoothie!\n","Peeling dragonfruit...\n","Blending dragonfruitin a blender...\n","Adding milk...\n","Enjoy a delicious dragonfruit smoothie!\n","\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"acThAznOc9t1"},"source":["## Classes\n","\n","Classes encapsulate attributes and methods. For instance, I may have an `Animal` class and instantiate class objects like `Cat` and `Dog`.\n","\n","Class syntax is as follows. Note that the `self` parameter is necessary for class functions (also known as class methods)\n","\n","```python\n","class MyClass():\n","    def __init__(self, param1, param2, ...):\n","      # something\n","\n","    def some_method(self, ...):\n","      # something\n","```"]},{"cell_type":"code","metadata":{"id":"HOyEBe4wc8_p","executionInfo":{"status":"ok","timestamp":1623875825513,"user_tz":420,"elapsed":277,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["class Animal():\n","  def __init__(self, name, speech):\n","    self.name = name\n","    self.speech = speech\n","    self.health = 50\n","\n","  def greet(self):\n","    # string concatenation\n","    greeting = self.name + ' says ' + self.speech\n","    return greeting\n","  \n","  def add_health(self, value):\n","    self.health += value\n","    # but need to make sure self.health is between 0 and 100\n","    self.health = max(0, self.health) # if smaller than 0, use 0\n","    self.health = min(100, self.health) # if larger than 100, use 100\n","    # no return value"],"execution_count":146,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"VzcCwg54lsyw","executionInfo":{"status":"ok","timestamp":1623877959829,"user_tz":420,"elapsed":268,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"19434bd6-9a36-4d42-de37-91b3a390266d"},"source":["dog = Animal('Doggie', 'Woof!')\n","dog.greet()"],"execution_count":157,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Doggie says Woof!'"]},"metadata":{"tags":[]},"execution_count":157}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"JBaQ3zTXl0ax","executionInfo":{"status":"ok","timestamp":1623877974255,"user_tz":420,"elapsed":155,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"233ff200-79ce-4602-9867-665f8d49f99d"},"source":["dog.health"],"execution_count":158,"outputs":[{"output_type":"execute_result","data":{"text/plain":["50"]},"metadata":{"tags":[]},"execution_count":158}]},{"cell_type":"markdown","metadata":{"id":"cVL_QdZ-l8kG"},"source":["We can either directly access class variables (known as class attributes) or, as I'd recommend, use class method to modify those attributes."]},{"cell_type":"code","metadata":{"id":"Ol-_irs4l2Eo","executionInfo":{"status":"ok","timestamp":1623878002596,"user_tz":420,"elapsed":185,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}}},"source":["dog.health += 20"],"execution_count":159,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"tkC2jEK4mJdS","executionInfo":{"status":"ok","timestamp":1623878004967,"user_tz":420,"elapsed":219,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"394d3d8b-52a0-438e-cbad-5825b182d215"},"source":["dog.health"],"execution_count":160,"outputs":[{"output_type":"execute_result","data":{"text/plain":["70"]},"metadata":{"tags":[]},"execution_count":160}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"27k5tS6DmLTv","executionInfo":{"status":"ok","timestamp":1623878018460,"user_tz":420,"elapsed":205,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"977045be-2eb0-4404-b599-9ff2322f3a26"},"source":["# but if we set dog.health manually\n","# there is no check to ensure that\n","# dog.health is always between 0 and 100\n","dog.health = -100\n","dog.health"],"execution_count":161,"outputs":[{"output_type":"execute_result","data":{"text/plain":["-100"]},"metadata":{"tags":[]},"execution_count":161}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"a2GwugUmma5v","executionInfo":{"status":"ok","timestamp":1623878032390,"user_tz":420,"elapsed":254,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"a0c43145-72b8-4b42-d298-5af0f7848fb1"},"source":["# the recommended way\n","dog = Animal('Doggie', 'Woof!')\n","dog.health"],"execution_count":162,"outputs":[{"output_type":"execute_result","data":{"text/plain":["50"]},"metadata":{"tags":[]},"execution_count":162}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_zKIwP5ymjkT","executionInfo":{"status":"ok","timestamp":1623878036424,"user_tz":420,"elapsed":220,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"da08df68-6179-49af-fa0b-b0bd114b173c"},"source":["dog.add_health(30)\n","dog.health"],"execution_count":163,"outputs":[{"output_type":"execute_result","data":{"text/plain":["80"]},"metadata":{"tags":[]},"execution_count":163}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QpV8mpN4moVM","executionInfo":{"status":"ok","timestamp":1623878041512,"user_tz":420,"elapsed":222,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"4065a8f7-447a-444c-bf82-b5de9722ab42"},"source":["dog.add_health(30)\n","dog.health"],"execution_count":164,"outputs":[{"output_type":"execute_result","data":{"text/plain":["100"]},"metadata":{"tags":[]},"execution_count":164}]},{"cell_type":"markdown","metadata":{"id":"WcpGq0ocmpOY"},"source":["We can instantiate more objects of the `Animal` class"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"iPZeC5i9muzr","executionInfo":{"status":"ok","timestamp":1623878070773,"user_tz":420,"elapsed":218,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"88b561d3-d6fc-4c8e-b147-efef63527ec6"},"source":["cat = Animal('Kitten', 'Meow!')\n","cat.greet()"],"execution_count":165,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Kitten says Meow!'"]},"metadata":{"tags":[]},"execution_count":165}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":37},"id":"OZLMgoKum0p-","executionInfo":{"status":"ok","timestamp":1623878082609,"user_tz":420,"elapsed":201,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"4c3cbb9e-0912-4df4-f839-99521b8ffbc1"},"source":["duck = Animal('Duckling', 'Quack!')\n","duck.greet()"],"execution_count":166,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Duckling says Quack!'"]},"metadata":{"tags":[]},"execution_count":166}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"PzWFu2U0oHZj","executionInfo":{"status":"ok","timestamp":1623878088196,"user_tz":420,"elapsed":202,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"5fa98209-6020-4c12-8181-cbc53b4170db"},"source":["duck.health"],"execution_count":167,"outputs":[{"output_type":"execute_result","data":{"text/plain":["50"]},"metadata":{"tags":[]},"execution_count":167}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"050drLrAoI4P","executionInfo":{"status":"ok","timestamp":1623878092782,"user_tz":420,"elapsed":177,"user":{"displayName":"Lynn Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiQigzOEmO4oYFGjhIk8_f7ztMxXvFAhyBTmwA78A=s64","userId":"10624347871689103070"}},"outputId":"b6315b8b-66f2-41e2-d430-5dc6b77a731f"},"source":["duck.add_health(20)\n","duck.health"],"execution_count":168,"outputs":[{"output_type":"execute_result","data":{"text/plain":["70"]},"metadata":{"tags":[]},"execution_count":168}]},{"cell_type":"markdown","metadata":{"id":"u8imWl1XYJ_A"},"source":["# End of Course Exercise\n"]},{"cell_type":"markdown","metadata":{"id":"_AAmrTmHdMie"},"source":["### 1. Write a function that prints a letter grade given a numerical score from 0-100, based on the scale 100 to 90 is A, 89 to 80 is B, 79 to 70 is C, 69 to 60 is D, and lower than 60 is F.\n"]},{"cell_type":"code","metadata":{"id":"1gt0ZaQmdu8M"},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"5axlSgM6dowa"},"source":["### 2. Write a function that determines whether an inputted list of numbers is sorted in ascending order (returns a True or False value).\n"]},{"cell_type":"code","metadata":{"id":"vfL9nfeMdvkl"},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"cX16mpOadwGI"},"source":["### 3. JSON objects are a type of data structure often used to store and transmit data. They consist of dictionaries and lists nested several levels deep. Given the following JSON object <code>donut_obj</code>, how can we get the number of toppings it has? How can we get a list of the types of toppings? Remember that lists are marked by [square brackets] and dictionaries are marked by {braces}.\n"]},{"cell_type":"code","metadata":{"id":"trhxmMvxd3vK"},"source":["donut = {\n","    \"id\": \"0001\",\n","    \"type\": \"donut\",\n","    \"name\": \"Cake\",\n","    \"ppu\": 0.55,\n","    \"batters\":\n","        {\n","            \"batter\":\n","                [\n","                    { \"id\": \"1001\", \"type\": \"Regular\" },\n","                    { \"id\": \"1002\", \"type\": \"Chocolate\" },\n","                    { \"id\": \"1003\", \"type\": \"Blueberry\" },\n","                    { \"id\": \"1004\", \"type\": \"Devil's Food\" }\n","                ]\n","        },\n","    \"topping\":\n","        [\n","            { \"id\": \"5001\", \"type\": \"None\" },\n","            { \"id\": \"5002\", \"type\": \"Glazed\" },\n","            { \"id\": \"5005\", \"type\": \"Sugar\" },\n","            { \"id\": \"5007\", \"type\": \"Powdered Sugar\" },\n","            { \"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\" },\n","            { \"id\": \"5003\", \"type\": \"Chocolate\" },\n","            { \"id\": \"5004\", \"type\": \"Maple\" }\n","        ]\n","}"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"TIVJwUfXeESa"},"source":["## Solutions"]},{"cell_type":"code","metadata":{"id":"ebRTfeKmdMif","colab":{"base_uri":"https://localhost:8080/","height":34},"executionInfo":{"status":"ok","timestamp":1568627370173,"user_tz":-480,"elapsed":994,"user":{"displayName":"Ruolin Zheng","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mADEfCxmn5NUmQJIzo_8IDp4C2r3o5rtT2hVwe4=s64","userId":"11795925282074014322"}},"outputId":"4b6dc4f4-977a-4cc2-f463-326432ce63c5"},"source":["# 1.\n","def letter_grade(score): # or use the grades dict we have!\n","    if score >= 90:\n","        print('A')\n","    elif score >= 80:\n","        print('B')\n","    elif score >= 70:\n","        print('C')\n","    elif score >= 60:\n","        print('D')\n","    else:\n","        print('F')\n","letter_grade(89)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["B\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"kW_IrmZNeP7G"},"source":["# 2.\n","def is_ascending(num_list):\n","    for i in range(len(num_list) - 1):\n","        # If a number in the list is greater than the one after, the list is not in ascending order\n","        if num_list[i] > num_list[i + 1]: \n","            return False # returning will automatically stop the function from executing further\n","    return True\n","\n","is_ascending([2, 3, 4, 6, 1])"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"LMRZWoZgeO-q"},"source":["# 3.\n","num_toppings = len(donut_obj[\"topping\"])\n","print(\"number of toppings: {}\".format(num_toppings))\n","\n","toppings_list = [] # empty list\n","for topping_info in donut_obj[\"topping\"]:\n","    toppings_list.append(topping_info[\"type\"])\n","print(\"list of toppings: {}\".format(toppings_list))"],"execution_count":null,"outputs":[]}]}